<?php

namespace App\Http\Servers\Admin;

use App\Fragment;

class FragmentServer
{
    private $fragment;

    public function __construct(Fragment $fragment)
    {
        $this->fragment = $fragment;
    }

    //需要管理员 / 内容盒子作者权限
    public function noPower()
    {
        return false;
        if (!((bool)\Auth::guard('admin')->user()->is_root)) {
            return message(false, '操作失败', '该操作需要超级管理员的权限!');
        } else {
            return false;
        }
    }

    //修改 开关 热门 推片 新品状态
    public function changeStatus($request)
    {
        if ($request->has('change')) {
            if ($message['message'] = $this->noPower()) {
                return back()->with($message);
            }
            $fragment = $this->fragment->findorfail($request->id);
            $change = $request->change;

            if (array_search($change, ['status', 'is_new', 'is_push', 'is_hot']) === false) {
                $message['message'] = message(false, '修改失败', '信息碎片该属性无法修改!');
                return back()->with($message);
            }

            $fragment->$change = (bool)$fragment->$change ? 0 : 1;
            $result = $fragment->save();
            if ($result) {
                $message['message'] = message(true, '修改成功', '信息碎片 ' . $fragment->name . ' 状态修改成功!');
            } else {
                $message['message'] = message(fasle, '修改失败', '信息碎片 ' . $fragment->name . ' 状态修改失败!');
            }
            $message['page'] = $request->page;
            return back()->with($message);
        }
    }

    //删除碎片
    public function delFragment($request)
    {
        if ($request->has('del')) {
            if ($message['message'] = $this->noPower()) {
                return back()->with($message);
            }

            $fragment = $this->fragment->findorfail($request->del);
            $result = $fragment->delete();
            if ($result) {
                $message['message'] = message(true, '删除成功', '信息碎片 ' . $fragment->name . ' 删除成功!');
            } else {
                $message['message'] = message(fasle, '删除失败', '信息碎片 ' . $fragment->name . ' 删除失败!');
            }
            $message['page'] = $request->page;
            return back()->with($message);
        }
    }
}